<?php
class eKonLib{

	public $sqlink = "";
	public $sqdb = "";
	
	public function __construct(){
		
	}

	public function init_db($host, $user, $pass){
		$this->sqlink = mysql_connect($host, $user, $pass);
		return $this->sqlink;
	}

	public function use_db($db_name){
		$this->sqdb = mysql_select_db($db_name , $this->sqlink);
		return $this->sqdb;
	}

	public function br_out($var){
		$returned = stripslashes(nl2br(strip_tags($var)));
		return $returned;
	}

	public function db_in($var){
		$returned = strip_tags(mysql_real_escape_string($var));
		return $returned;
	}

	public function db_out($var){
		$returned = stripslashes(strip_tags(stripslashes($var)));
		return $returned;
	}

	public function db_cmp($var){
		$returned = strip_tags($var);
		return $returned;
	}

	public function captcha($code){
		$largeur=98;
		$hauteur=25;
		$img = imagecreate($largeur, $hauteur) or die("Cannot Initialize new GD image stream");

		$bgc = imagecolorallocate($img, mt_rand(235,255), mt_rand(235,255), mt_rand(235,255));
		$black = imagecolorallocate($img, 0, 0, 0);

		imagefilledrectangle($img, 0, 0, $largeur, $hauteur, $bgc);

		$hor_pos=mt_rand(5,15); // horizontal position
		for($i=0;$i<strlen($code);$i++){
			imagestring($img, 5, $hor_pos, mt_rand(2,10), $code[$i], $black);
			$hor_pos+=mt_rand(10,20);
		}

		for($i=0;$i<70;$i++){
			imagesetpixel($img, mt_rand(0,$largeur), mt_rand(0,$hauteur), imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
		}

		for($i=0;$i<1;$i++){
			if($i<2){
				$x1=rand(0,$largeur);
				$y1=0;
				$x2=abs($x1-mt_rand(0,5));
				$y2=$hauteur;
			}else{
				$x1=0;
				$y1=rand(0,$hauteur);
				$x2=$largeur;
				$y2=abs($y1-mt_rand(0,5));
			}
			imageline($img, $x1, $y1, $x2, $y2, $black);
		}


		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		header("Cache-Control: no-store, no-cache, must-revalidate"); 
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		header("Content-Type: image/png");
		imagePNG($img);
		imagedestroy($img);
	}

}

class Local extends eKonLib{
	public $limit;
	public $tax;

	public function is_duplicate($username){
		
		
		$sel = mysql_query("SELECT user_ID FROM users WHERE user_username = '" . $this->db_cmp($username) . "'");
		$res = mysql_fetch_array($sel);
		if($res['user_ID']){
			return true;
		}else{
			return false;
		}
		

	}

	
	public function input_field($text , $name , $value, $label_div_id , $pos_left , $pos_top , $size = "small" , $arrow = true , $extra_events = ""){
		if($size == "small"){$bkg = "images/bk_input.png"; $width="170"; $that_left = "10"; $arrow_left="140"; $input_size="120";}else{$bkg="images/big_bk_input.png"; $width="240"; $that_left = "12"; $arrow_left="210"; $input_size="195";}
		?>
		<div style="position:absolute; top:<?=$pos_top?>px; left:<?=$pos_left?>px;">
			<div style="position:absolute; background-image:url('<?=$bkg?>'); width:<?=$width?>px; height:26px;">

				<?if($arrow == true){?>
				<div style="position:absolute; cursor:pointer; left:<?=$arrow_left?>px; top:5px; background-image:url('images/drop_down_arrow.gif'); width:17px; height:15px;"
				onClick="document.getElementById('<?=$name?>').focus();"
				></div>
				<?}?>

				<div class="input_div" style="position:absolute; left:<?=$that_left?>px; top:4px;">
					<input class="input_div" id="<?=$name?>" type="text" name="<?=$name?>" style="border:0px solid black; background:white; color:black; width:<?=$input_size?>px; font-size:12px;" 
					onFocus="if(document.getElementById('<?=$name?>').value == '' ){swap('<?=$label_div_id?>');} 
					if(document.getElementById('<?=$name?>').value != '' ){fadeOut('<?=$label_div_id?>');}
					" 
					onBlur="if(document.getElementById('<?=$name?>').value == '' ){swap('<?=$label_div_id?>');} 
					if(document.getElementById('<?=$name?>').value != '' ){fadeOut('<?=$label_div_id?>');}
					" 
					<?=$extra_events?>
					

					value="<?=$this->db_out($value)?>" />
				</div>

				<?if(!$_GET[$name]){$disp = "block";}else{$disp="none";}?>
				<div class="input_div" id="<?=$label_div_id?>" style="position:absolute; display:<?=$disp?>; font-size:10px; font-family:tahoma; color:#A4A4A4; width:100px; top:7px; left:9px;" onClick="document.getElementById('<?=$name?>').focus();"><?=$text?></div>

			</div>
		</div>
		<?
	}



	public function is_captcha($code){
		
		
		if($code == $_SESSION['code']){
			return true;
		}else{
			return false;
		}
		

	}


	public function is_logged(){
		if($_SESSION['uid']){
			return true;
		}else{
			return false;
		}
	}

	public function check_session($err_url){
		if(!$_SESSION['uid']){
			echo "<script> alert('Your session has expired. Please login.'); location.href='" . $err_url . "'; </script>";
			exit;
		}
	}



	public function date_to_timestamp($date){

		$dateArr = explode("/" , $date);
		$month = $dateArr[0];
		$day   = $dateArr[1];
		$year  = $dateArr[2];

		$ret = @mktime(0 , 0 , 0 , $month , $day , $year);

		return $ret;
	

	}

	public function age($date){

		$dateArr = explode("/" , $date);
		$year  = $dateArr[2];

		$age = date("Y" , time()) - $year;

		return $age;
	

	}

	public function user($uid){
		$sel = mysql_query("SELECT * FROM users WHERE user_ID = '" . $this->db_cmp($uid) . "'");
		$res = mysql_fetch_array($sel);

		return $res;
	}
}

function thumb($rim , $sz , $dest , $quality = 25){
	$imgDat = getimagesize($rim);
	$mime = $imgDat['mime'];
	   
	if ($imgDat[0] > $imgDat[1]+20) {
	  $tw = $sz;
	  $th = $imgDat[1]/($imgDat[0]/$tw);
	} else {
	  $th = $sz;
	  $tw = $imgDat[0]/($imgDat[1]/$th);
	}

	if ($sz == "-1") {
		$imNew = ImageCreateTrueColor($imgDat[0], $imgDat[1]);
	}else{
		$imNew = ImageCreateTrueColor($tw, $th);
	}
	   
	if ($mime == 'image/gif') {
	  $imFile= ImageCreateFromGIF($rim);
	} else if ($mime == 'image/png') {
	  $imFile= ImageCreateFromPNG($rim);
	} else if ($mime == 'image/jpeg') {
	  $imFile= ImageCreateFromJpeg($rim);
	}
	  
	if ($sz == "-1") {
	  imagecopyresampled ($imNew, $imFile, 0, 0, 0, 0, $imgDat[0], $imgDat[1], $imgDat[0], $imgDat[1]);
	} else {
	  imagecopyresampled ($imNew, $imFile, 0, 0, 0, 0, $tw, $th, $imgDat[0], $imgDat[1]);
	}

	imagejpeg($imNew, $dest, $quality);

	imagedestroy($imNew);
}

class MyPagina {
	
	var $sql;
	var $result;
	
	var $get_var = "page";
	var $rows_on_page;
	var $str_forward;
	var $str_backward;
	var $max_links;
	var $module;
	
	var $all_rows;
	var $num_rows;
	
	var $page;
	var $number_pages;
	
	// constructor
	function MyPagina() {
		//$this->connect_db();
		echo mysql_error();
	}
	// sets the current page number
	function set_page() {
		$this->page = (isset($_REQUEST[$this->get_var]) && $_REQUEST[$this->get_var] != "") ? $_REQUEST[$this->get_var] : 0;
		return $this->page;
	}
	// gets the total number of records 
	function get_total_rows() {
		$tmp_result = mysql_query($this->sql);
		$this->all_rows = mysql_num_rows($tmp_result);
		mysql_free_result($tmp_result);
		return $this->all_rows;
	}
	// database connection
	function connect_db() {
		//$conn_str = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
		//mysql_select_db(DB_NAME, $conn_str);
	}
	// get the totale number of result pages
	function get_num_pages() {
		$this->number_pages = ceil($this->get_total_rows() / $this->rows_on_page);
		return $this->number_pages;
	}
	// returns the records for the current page
	function get_page_result() {
		$start = $this->set_page() * $this->rows_on_page;
		$page_sql = sprintf("%s LIMIT %s, %s", $this->sql, $start, $this->rows_on_page);
		$this->result = mysql_query($page_sql);
		return $this->result;
	}
	// get the number of rows on the current page
	function get_page_num_rows() {
		$this->num_rows = mysql_num_rows($this->result);
		return $this->num_rows;
	}
	// free the database result
	function free_page_result() {
		mysql_free_result($this->result);
	}
	// function to handle other querystring than the page variable
	function rebuild_qs($curr_var) {
		if (!empty($_SERVER['QUERY_STRING'])) {
			$parts = explode("&", $_SERVER['QUERY_STRING']);
			$newParts = array();
			foreach ($parts as $val) {
				if (stristr($val, $curr_var) == false)  {
					array_push($newParts, $val);
				}
			}
			if (count($newParts) != 0) {
				$qs = "&".implode("&", $newParts);
			} else {
				return false;
			}
			return $qs; // this is your new created query string
		} else {
			return false;
		}
	} 
	// this method will return the navigation links for the conplete recordset
	function navigation($separator = " | ", $css_current = "", $back_forward = false) {
		$curr_pages = $this->set_page(); 
		$all_pages = $this->get_num_pages() - 1;
		$var = $this->get_var;
		$navi_string = "";
		if (!$back_forward) {
			$this->max_links = ($this->max_links < 2) ? 2 : $this->max_links;
		}
		if ($curr_pages <= $all_pages && $curr_pages >= 0) {
			if ($curr_pages > ceil($this->max_links/2)) {
				$start = ($curr_pages - ceil($this->max_links/2) > 0) ? $curr_pages - ceil($this->max_links/2) : 1;
				$end = $curr_pages + ceil($this->max_links/2);
				if ($end >= $all_pages) {
					$end = $all_pages + 1;
					$start = ($all_pages - ($this->max_links - 1) > 0) ? $all_pages  - ($this->max_links - 1) : 1;
				}
			} else {
				$start = 0;
				$end = ($all_pages >= $this->max_links) ? $this->max_links : $all_pages + 1;
			}
			if($all_pages >= 1) {
				$forward = $curr_pages + 1;
				$backward = $curr_pages - 1;
				$navi_string = ($curr_pages > 0) ? "<a href=\"?".$var."=".$backward.$this->rebuild_qs($var)."\">".$this->str_backward."</a>&nbsp;" : $this->str_backward."&nbsp;";
				if (!$back_forward) {
					for($a = $start + 1; $a <= $end; $a++){
						$theNext = $a - 1; // because a array start with 0
						if ($theNext != $curr_pages) {
							//$navi_string .= "<a href=\"".$_SERVER['PHP_SELF']."?".$var."=".$theNext.$this->rebuild_qs($var)."\">";
							//$navi_string .= $a."</a>";

							$navi_string .= "<a href=\"?".$var."=".$theNext.$this->rebuild_qs($var)." \">";
							$navi_string .= $a."</a>";

							$navi_string .= ($theNext < ($end - 1)) ? $separator : "";
						} else {
							$navi_string .= ($css_current != "") ? "<span class=\"".$css_current."\">".$a."</span>" : $a;
							$navi_string .= ($theNext < ($end - 1)) ? $separator : "";
						}
					}
				}
				$navi_string .= ($curr_pages < $all_pages) ? "&nbsp;<a href=\"?".$var."=".$forward.$this->rebuild_qs($var)."\">".$this->str_forward."</a>" : "&nbsp;".$this->str_forward;
			}
		}
		return $navi_string;
	}
	
	function page_info($to = "-") {
		$first_rec_no = ($this->set_page() * $this->rows_on_page) + 1;
		$last_rec_no = $first_rec_no + $this->rows_on_page - 1;
		$last_rec_no = ($last_rec_no > $this->get_total_rows()) ? $this->get_total_rows() : $last_rec_no;
		$to = trim($to);
		$info = $first_rec_no." ".$to." ".$last_rec_no;
		return $info;
	}
	// simple method to show only the page back and forward link.
	function back_forward_link() {
		$simple_links = $this->navigation(" ", "", true);
		return $simple_links;
	}
}




class Paginator extends MyPagina{
	public $nav_links = "";
	public $nav_info = "";
	public $simple_nav_links = "";
	public $total_recs = "";

	public function Paginate($sqlQuery , $cssLinkClass , $linkSeparator , $navInfo , $num_records , $max_pages , $back_txt , $for_txt){
		$this->rows_on_page = $num_records;
		$this->max_links = $max_pages;
		$this->str_backward = $back_txt;
		$this->str_forward = $for_txt;

		$this->sql = $sqlQuery;
		$this->result = $this->get_page_result();
		$this->num_rows = $this->get_page_num_rows();
		$this->nav_links = $this->navigation($linkSeparator, $cssLinkClass);
		$this->nav_info = $this->page_info($navInfo);
		$this->simple_nav_links = $this->back_forward_link();
		$this->total_recs = $this->get_total_rows();

		return array("num_rows"=>$this->num_rows , "nav_links"=>$this->nav_links , "nav_info"=>$this->nav_info , "total_recs"=>$this->total_recs);
	}
	
	public function setRow($filedName , $i){
		return @strip_tags(@mysql_real_escape_string(@mysql_result($this->result, $i, $filedName)));
	}

}
?>